/**
 * Created by xgjiang on 2015-10-10.
 */
define([
        'angular',
        'adConfig',
        'app/modules/users/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.users')
            .factory("adUsersService", ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();

                    var url = adConfig.baseServiceUrl + 'user/users/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function addItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/users/add';
                    adRequest(url, {
                        userName: row.user_name, userPwd: row.user_password, userAlias: row.user_alias
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function updateItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/users/update';
                    adRequest(url, {
                        userId: row.user_id,
                        userName: row.user_name,
                        userPwd: row.user_password,
                        userAlias: row.user_alias
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function deleteItems(rows) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/users/delete';
                    var userIds = _.map(rows, function (row) {
                        return row.user_id;
                    });
                    adRequest(url, {
                        userIds: angular.toJson(userIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function checkUserLogin(id, checkValue) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/users/checkUsrLoginUnique';
                    adRequest(url, {
                        userId: id,
                        userName: checkValue
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function getAllRoles() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/role/query';

                    adRequest(url).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function getUsrRoles(userId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/permissions/queryUsrRoles';
                    //var userId = _.map(rows,function(row){return row.user_id;});
                    adRequest(url, {
                        userId: userId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function saveUsrRole(userId, roleIds) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/permissions/saveUsrRoles';
                    adRequest(url, {
                        userId: userId,
                        roleIds: roleIds
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                return {
                    getPage: getPage,
                    addItem: addItem,
                    updateItem: updateItem,
                    deleteItems: deleteItems,
                    checkUserLogin: checkUserLogin,
                    getAllRoles: getAllRoles,
                    getUsrRoles: getUsrRoles,
                    saveUsrRole: saveUsrRole
                };
            }]);
    });